//
//  MenuHomeViewController.m
//  FMS
//
//  Created by Steven on 10/10/13.
//  Copyright (c) 2013 QGSVN. All rights reserved.
//

#import "MenuHomeViewController.h"
#import "ListScheduleViewController.h"
#import "MaintenanceMgmtViewController.h"
#import "BuildingMgmtViewController.h"
#import "ListLiaisonViewController.h"

@interface MenuHomeViewController ()

@end

@implementation MenuHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    dictLanguage = [ApplicationDelegate.arrayLanguage objectAtIndex:0];

    _btnLogOut.titleLabel.font = [UIFont fontWithName:FMS_REGULAR_FONT size:17];
    [_btnLogOut setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
    [_btnLogOut setTitleEdgeInsets:UIEdgeInsetsMake(0, -45, 0, 0)];
    [_btnLogOut setTitleColor:APPLE_BLUE_COLOR forState:UIControlStateNormal];
    [_btnLogOut setTitle:[dictLanguage objectForKey:@"logout.title"] forState:UIControlStateNormal];
    [_btnLogOut setImage:[UIImage imageNamed:@"iconLogOut.png"] forState:UIControlStateNormal];
    [_btnLogOut setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -145)];
    
    //alloc array to store Menu List data
    scrollMenu = [[UIScrollMenu alloc] initWithFrame:CGRectMake(0, 120, 1024, 588) parent:self];
    [self.view addSubview:scrollMenu];
    scrollMenu.arrayMenu = [[NSMutableArray alloc] init];
}

- (void) viewWillAppear:(BOOL)animated {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[NSNumber numberWithInt:0] forKey:@"isSelectDetail"];
    
    self.navigationController.navigationBarHidden = NO;
    self.lblAccountEmail.text = [[NSUserDefaults standardUserDefaults] objectForKey:USER_EMAIL];
    
    [scrollMenu.arrayMenu removeAllObjects];
    
    //init data for Menu list
    NSMutableDictionary *item1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:TAB_BUILDING_MGMT],@"index",[dictLanguage objectForKey:@"home.building-mgmt"],CATEGORY_NAME,@"btnMenuBuilding.png",CATEGORY_IMAGE,@"btnMenuBuildingSelected.png",CATEGORY_IMAGE_SELECTED,nil];
    [scrollMenu.arrayMenu addObject:item1];
    
    NSMutableDictionary *item2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:TAB_FACILITY_MGMT],@"index",[dictLanguage objectForKey:@"home.facility-mgmt"],CATEGORY_NAME,@"btnMenuFacility.png",CATEGORY_IMAGE,@"btnMenuFacilitySelected.png",CATEGORY_IMAGE_SELECTED,nil];
    [scrollMenu.arrayMenu addObject:item2];
    
    NSMutableDictionary *item3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:TAB_MAINTENANCE_MGMT],@"index",[dictLanguage objectForKey:@"home.maintenance-mgmt"],CATEGORY_NAME,@"btnMenuMaintenance.png",CATEGORY_IMAGE,@"btnMenuMaintenanceSelected.png",CATEGORY_IMAGE_SELECTED,nil];
    [scrollMenu.arrayMenu addObject:item3];
    
    NSMutableDictionary *item4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:TAB_SCHEDULING],@"index",[dictLanguage objectForKey:@"home.scheduling"],CATEGORY_NAME,@"btnMenuScheduling.png",CATEGORY_IMAGE,@"btnMenuSchedulingSelected.png",CATEGORY_IMAGE_SELECTED,nil];
    [scrollMenu.arrayMenu addObject:item4];
    
    NSMutableDictionary *item5 = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:TAB_LIAISON_MGMT],@"index",[dictLanguage objectForKey:@"home.liaison-mgmt"],CATEGORY_NAME,@"btnMenuLiaison.png",CATEGORY_IMAGE,@"btnMenuLiaisonSelected.png",CATEGORY_IMAGE_SELECTED,nil];
    [scrollMenu.arrayMenu addObject:item5];
    
    NSMutableDictionary *item6 = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:TAB_REPORTING],@"index",[dictLanguage objectForKey:@"home.reporting"],CATEGORY_NAME,@"btnMenuReporting.png",CATEGORY_IMAGE,@"btnMenuReportingSelected.png",CATEGORY_IMAGE_SELECTED,nil];
    [scrollMenu.arrayMenu addObject:item6];
    
    [scrollMenu setupPage];
}

- (void) btnMenuClick:(id)sender {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    int index = ((UIButton*)sender).tag;
    switch (index) {
        case TAB_BUILDING_MGMT:
        {
            BuildingMgmtViewController *controller = [[BuildingMgmtViewController alloc] initWithNibName:nil bundle:nil itemType:Building_Type];
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
            ApplicationDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navController,nil];
            [ApplicationDelegate.menuController presentModalViewController:ApplicationDelegate.tabBarController animated:YES];
            break;
        }
        case TAB_FACILITY_MGMT:
        {
            BuildingMgmtViewController *controller = [[BuildingMgmtViewController alloc] initWithNibName:nil bundle:nil itemType:Facility_Type];
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
            ApplicationDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navController,nil];
            [ApplicationDelegate.menuController presentModalViewController:ApplicationDelegate.tabBarController animated:YES];
            break;
        }
        case TAB_MAINTENANCE_MGMT:
        {
            //check if don't have both of Inspection and Maintenance Access permission
            NSMutableDictionary *maintenanceMgmtDict = [userDefaults objectForKey:MAINTENANCE_PERMISSION];
            BOOL isAccessInspection = [[[maintenanceMgmtDict objectForKey:@"inspection"] objectForKey:@"access"] boolValue];
            BOOL isAccessMaintenance = [[[maintenanceMgmtDict objectForKey:@"maintenance"] objectForKey:@"access"] boolValue];
            
            if (!isAccessInspection && !isAccessMaintenance) {
                UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:[dictLanguage objectForKey:@"general.warning"] message:[dictLanguage objectForKey:@"general.permission.no-right-access"] delegate:nil cancelButtonTitle:[dictLanguage objectForKey:@"general.ok"] otherButtonTitles:nil];
                [dialog show];
                return;
            }
            
            MaintenanceMgmtViewController *controller = [[MaintenanceMgmtViewController alloc] initWithNibName:nil bundle:nil];
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
            ApplicationDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navController,nil];
            [ApplicationDelegate.menuController presentModalViewController:ApplicationDelegate.tabBarController animated:YES];
            break;
        }
        case TAB_SCHEDULING:
        {
            //Get permission about this tab
            //if has Edit permission is also has Add permission
            NSMutableDictionary *permissionDict = [userDefaults objectForKey:SCHEDULE_PERMISSION];
            BOOL isAccess = [[permissionDict objectForKey:@"access"] boolValue];
            if (!isAccess) {
                UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:[dictLanguage objectForKey:@"general.warning"] message:[dictLanguage objectForKey:@"general.permission.no-right-access"] delegate:nil cancelButtonTitle:[dictLanguage objectForKey:@"general.ok"] otherButtonTitles:nil];
                [dialog show];
                return;
            }
            
            ListScheduleViewController *controller = [[ListScheduleViewController alloc] initWithNibName:@"ListScheduleViewController" bundle:[NSBundle mainBundle]];
            
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
            ApplicationDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, nil];
            [ApplicationDelegate.menuController presentModalViewController:ApplicationDelegate.tabBarController animated:YES];
            
        }
            break;
        case TAB_LIAISON_MGMT:   
        {
            //Get permission about this tab
            //if has Edit permission is also has Add permission
            NSMutableDictionary *permissionDict = [userDefaults objectForKey:LIAISON_PERMISSION];
            BOOL isAccess = [[permissionDict objectForKey:@"access"] boolValue];
            if (!isAccess) {
                UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:[dictLanguage objectForKey:@"general.warning"] message:[dictLanguage objectForKey:@"general.permission.no-right-access"] delegate:nil cancelButtonTitle:[dictLanguage objectForKey:@"general.ok"] otherButtonTitles:nil];
                [dialog show];
                return;
            }
            
            ListLiaisonViewController *controller = [[ListLiaisonViewController alloc] initWithNibName:@"ListLiaisonViewController" bundle:[NSBundle mainBundle]];
            
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
            ApplicationDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, nil];
            [ApplicationDelegate.menuController presentModalViewController:ApplicationDelegate.tabBarController animated:YES];
        }
            break;
        default:
        {
            UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"This function is unavailable in this version" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [dialog show];
            break;
            
            //Get permission about this tab
            //if has Edit permission is also has Add permission
            NSMutableDictionary *permissionDict = [userDefaults objectForKey:REPORTING_PERMISSION];
            BOOL isAccess = [[permissionDict objectForKey:@"access"] boolValue];
            if (!isAccess) {
                UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:[dictLanguage objectForKey:@"general.warning"] message:[dictLanguage objectForKey:@"general.permission.no-right-access"] delegate:nil cancelButtonTitle:[dictLanguage objectForKey:@"general.ok"] otherButtonTitles:nil];
                [dialog show];
                return;
            }
        }
    }
}

- (IBAction)tapLogOut:(id)sender {
    UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:[dictLanguage objectForKey:@"logout.title"] message:[dictLanguage objectForKey:@"logout.message"] delegate:self cancelButtonTitle:[dictLanguage objectForKey:@"general.no"] otherButtonTitles:[dictLanguage objectForKey:@"general.yes"], nil];
    [dialog show];
}

#pragma mark UIAlertViewDelegate
#pragma mark - UIAlerview delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
    {
//        //clear cookies in reporting
//        [[NSURLCache sharedURLCache] removeAllCachedResponses];
//        
//        NSHTTPCookie *cookie;
//        NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
//        for (cookie in [storage cookies]) {
//            [storage deleteCookie:cookie];
//        }
//        [[NSUserDefaults standardUserDefaults] synchronize];
        
        AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        [delegate.navigationController popToRootViewControllerAnimated:NO];
        delegate.window.rootViewController = delegate.navigationController;
    }
}

@end
